const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('src/cyc.db')
import sqlOption from './sqlOption'
// 显示所有表
export function getTableList() {
  return new Promise((resolve, reject) => {
    db.serialize(() => {
      db.all(sqlOption.allTableSql(), (err, rows) => {
        if (err) {
          reject(err)
          return console.error(err.message);
        } else {
          resolve(rows)
        }
      })
    })
  })
}

// 数据结构
export function checkData(model) {
  return new Promise((resolve, reject) => {
    db.serialize(() => {
      db.all(sqlOption.infoSql(model), (err, rows) => {
        if (err) {
          reject(err)
          throw err;
        } else {
          resolve(rows)
        }
      });
    });
  });
}

// 获取全部数据
export function getAllData(table) {
  return new Promise((resolve, reject) => {
    let sql = sqlOption.allDataSql(table)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (err) {
          reject(err)
          throw err;
        } else {
          resolve({
            sql,
            code: 1000,
            total: rows.length,
            data: rows
          })
        }
      });
    });
  });
}

// 属性列表
export function getColumnList(table) {
  return new Promise((resolve, reject) => {
    let sql = sqlOption.columnsSql(table)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (err) {
          reject(err)
          return console.error(err.message);
        } else {
          resolve({
            sql,
            code: 1000,
            total: rows.length,
            data: rows
          })
        }
      })
    })
  })
}

// 查找数据
export function queryData(model) {
  return new Promise((resolve, reject) => {
    let sqlObj = sqlOption.querySql(model)
    db.serialize(() => {
      db.all(sqlObj.all, (err, rows) => {
        if (!err) {
          db.all(sqlObj.sql, (err2, rows2) => {
            if (!err2) {
              resolve({
                sql: sqlObj.sql,
                code: 1000,
                total: rows.length,
                data: rows2
              })
            } else {
              reject(err2)
            }
          })
        } else {
          reject(err)
        }
      })
    })
  })
}

// 添加数据
export function addData(model) {
  return new Promise((resolve, reject) => {
    const sql = sqlOption.addSql(model)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (!err) {
          if (rows.length == 0) {
            resolve({
              code: 1000,
              msg: '添加成功'
            })
          }
        } else {
          reject(err)
        }
      })
    })
  })
}

// 编辑数据
export function editData(model) {
  return new Promise((resolve, reject) => {
    const sql = sqlOption.editSql(model)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (!err) {
          if (rows.length == 0) {
            resolve({
              code: 1000,
              data: model.data,
              msg: '编辑成功'
            })
          }
        } else {
          reject(err)
        }
      })
    })
  })
}

// 删除数据
export function delData(table, id) {
  return new Promise((resolve, reject) => {
    const sql = sqlOption.delSql(model)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (!err) {
          if (rows.length == 0) {
            resolve({
              code: 1001,
              msg: '删除成功'
            })
          }
        } else {
          reject(err)
        }
      })
    })
  })
}

// 批量排序
export function batchSort(model) {
  return new Promise((resolve, reject) => {
    const sql = sqlOption.batchSortSql(model)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (!err) {
          if (rows.length == 0) {
            resolve({
              code: 1000,
              sql,
              msg: '操作成功'
            })
          }
        } else {
          reject(err)
        }
      })
    })
  })
}

// 树形排序
export function treeSort(model) {
  return new Promise((resolve, reject) => {
    const sql = sqlOption.treeSortSql(model)
    db.serialize(() => {
      db.all(sql, (err, rows) => {
        if (!err) {
          if (rows.length == 0) {
            resolve({
              code: 1000,
              sql,
              msg: '操作成功'
            })
          }
        } else {
          reject(err)
        }
      })
    })
  })
}
